package com.gzz.orchid.context.domain.rbac.impl;

import com.gzz.orchid.common.core.util.I18nUtil;
import com.gzz.orchid.context.domain.rbac.IRoleRepository;
import com.gzz.orchid.context.domain.rbac.IRoleService;
import com.gzz.orchid.context.domain.rbac.entity.RoleDo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author higmd
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RoleService implements IRoleService {
    private final IRoleRepository repository;

    @Override
    public void checkCodeUnique(RoleDo roleDo) throws IllegalArgumentException {
        RoleDo exsitsDo = repository.findByCode(roleDo.getCode());
        if (Objects.nonNull(exsitsDo) && !exsitsDo.getId().equals(roleDo.getId())) {
            throw new IllegalArgumentException(I18nUtil.ofMessage("角色编码[%s]已存在", roleDo.getCode()));
        }
    }

    @Override
    public void checkCodeExistence(String code) throws IllegalArgumentException {
        RoleDo exsitsDo = repository.findByCode(code);
        if (Objects.isNull(exsitsDo)) {
            throw new IllegalArgumentException(I18nUtil.ofMessage("角色编码[%s]不存在", code));
        }
    }
}
